package com.liuweiheng.base.aop;

import com.liuweiheng.base.utils.GlobalStrRedisUtil;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 检查全局接口幂等性
 * 检查token
 *
 * @author lwh
 */
@SuppressWarnings("Duplicates")
@Aspect
@Component
public class TokenAspect {

    @Pointcut("execution(public * com.liuweiheng.*.*.controller.*.*(..))")
    public void checkToken(){}

    /**
     * 检查token
     */
    @Before("checkToken()")
    public void deBefor() {
        // 接受请求
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = attributes.getRequest();

        String token = request.getHeader("SYS_INTERFACE_TOKEN");

//        String value = new GlobalStrRedisUtil().getValue("SYS:INTERFACE:TOKEN");


        /* 暂不不启用
        if (StrUtil.isBlank(value)) {
            throw new TokenException("token is not exist");
        }
        */

    }

    /**
     * 删除token
     */
    @After("checkToken()")
    public void deAfter() {
        boolean delKey = false;
        /* 暂不启用
        while (!delKey) {
            delKey = StrRedisUtil.delKey("SYS:INTERFACE:TOKEN");
        }
        */
    }





}
